﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Edu.Web.Data.DAO;
using Edu.Web.Data.DV;
using Edu.Web.Data.Entity;
using ESoft.Core.Data.Service.Impl;
using ESoft.Core.Interceptor;
using ESoft.Core.Utils.Extensions;

namespace Edu.Web.Data.Service.Impl
{
    public class ClazzService : BaseService<ClazzEntity, long>, IClazzService
    {
        private readonly IClazzDAO _clazzDao;
        private readonly IClazzTeacherDAO _clazzTeacherDao;
        private readonly ITeacherDAO _teacherDao;

        private readonly IUserService _userService;
        private readonly IClazzStuDAO _clazzStuDao;
        private readonly IStudentDAO _studentDao;

        public ClazzService(IClazzDAO clazzDao, IClazzTeacherDAO clazzTeacherDao, ITeacherDAO teacherDao, IUserService userService, IClazzStuDAO clazzStuDao, IStudentDAO studentDao) : base(clazzDao)
        {
            _clazzDao = clazzDao;
            _clazzTeacherDao = clazzTeacherDao;
            _teacherDao = teacherDao;
            _userService = userService;
            _clazzStuDao = clazzStuDao;
            _studentDao = studentDao;
        }

        public List<ClazzListItemDV> LoadClazzByUserId(long userId, long userType)
        {
            return _clazzDao.LoadDataByTeacherUid(userId).ToList();
        }

        public List<ClassStudentDV> LoadClazzStuByClazzId(long clazzId)
        {
            return _clazzDao.LoadStudentListByClazzId(clazzId).ToList();
        }

        public long SaveClazz(ClazzEntity clazzVO, long userId)
        {
            var teacher = _teacherDao.LoadEntities(x => x.UserId == userId).FirstOrDefault();
            if (teacher.IsNull())
            {
                throw new Exception("不存在该教师信息！");
            }
            clazzVO = _clazzDao.Add(clazzVO);

            ClazzTeacherEntity ct = new ClazzTeacherEntity();
            ct.TeacherUserId = teacher.UserId;
            ct.ClazzId = clazzVO.Id;
            ct.TeacherType = 1;
            _clazzTeacherDao.Add(ct);

            return clazzVO.Id;
        }
    }
}